package com.zork.cmdb.webapi.api;

import com.github.pagehelper.PageInfo;
import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.common.response.ResponseEntityEnum;
import com.zork.cmdb.webapi.service.AutoFindResultSevice;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author chenjh
 * @date 2018/6/26
 * @description
 */
@RestController
@RequestMapping("/autoFindResults")
public class AutoFindResultController {

    private static final Integer MAX_PAGE_SIZE = 200;

    @Autowired
    private AutoFindResultSevice autoFindResultSevice;

    @ApiOperation(value = "获取自动发现结果列表", notes = "获取自动发现结果列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", required = false, defaultValue = "10", dataType = "Integer")
    })
    @GetMapping("")
    public ResponseEntityEnum listAutoFindResult(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) throws CmdbException {
        if (pageSize > MAX_PAGE_SIZE) {
            return ResponseEntityEnum.PARAM_EROOR.setMsg("pageSize不能大于" + MAX_PAGE_SIZE);
        }
        PageInfo<Map<String, Object>> autoFindResult = autoFindResultSevice.listAutoFindResult(null, pageNum, pageSize);
        ResponseEntityEnum result = ResponseEntityEnum.SUCCESS.setData(autoFindResult);
        return result;
    }


    @ApiOperation(value = "根据remedy的GUID获取自动发现结果", notes = "根据remedy的GUID获取自动发现结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sourceInstanceID", value = "remedy的GUID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", required = false, defaultValue = "10", dataType = "Integer")
    })
    @GetMapping("/{sourceInstanceID}")
    public ResponseEntityEnum getAutoFindResultBySourceInstanceID(@PathVariable("sourceInstanceID") String sourceInstanceID,
                                                                  @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                                  @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) throws CmdbException {
        if (pageSize > MAX_PAGE_SIZE) {
            return ResponseEntityEnum.PARAM_EROOR.setMsg("pageSize不能大于" + MAX_PAGE_SIZE);
        }
        PageInfo<Map<String, Object>> autoFindResult = autoFindResultSevice.listAutoFindResult(sourceInstanceID, pageNum, pageSize);
        ResponseEntityEnum result = ResponseEntityEnum.SUCCESS.setData(autoFindResult);
        return result;
    }
}